package com.example.speeddating.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
//import jakarta.persistence.OneToOne;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:34
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 好友列表
     * @param userPage
     * @param userId
     * @return
     */
    @Select(value = "SELECT u.* FROM `user` u WHERE u.id IN \n" +
            "(SELECT follow_id FROM follow WHERE follow_id IN (SELECT user_id FROM follow WHERE follow_id = #{userId}) AND user_id = #{userId} and deleted = 0)")
    Page<User> selectFriendList(Page<User> userPage,Long userId);

    /**
     * 关注列表
     * @param userPage
     * @param userId
     * @return
     */
    @Select(value = "SELECT u.* FROM follow f,`user` u WHERE f.follow_id = u.id AND  f.user_id = #{userId} and u.deleted = 0 AND f.deleted = 0")
    Page<User> selectFollowList(Page<User> userPage,Long userId);


    /**
     * 未读数量
     */
    @Select(value = "SELECT count(*) FROM follow WHERE follow_id = #{userId} AND is_read = 0 AND deleted = 0")
    Integer unReadFansCount(Long userId);

    /**
     * 粉丝列表
     * @param userPage
     * @param userId
     * @return
     */
    @Select(value = "SELECT u.* FROM follow f,`user` u WHERE f.user_id = u.id AND  f.follow_id = #{userId} and u.deleted = 0 AND f.deleted = 0")
    Page<User> selectFansList(Page<User> userPage,Long userId);

    /**
     * 未读访客数量
     */
    @Select(value = "SELECT count(*) FROM visitor WHERE visitor_id = #{userId} AND is_read = 0")
    Integer unReadVisitCount(Long userId);

    /**
     * 查询是否关注该用户
     * @param userId
     * @param followId
     * @return
     */
    @Select(value = "SELECT count(*) FROM follow WHERE user_id = #{userId} AND follow_id = #{followId} AND deleted = 0")
    Integer selectIsFocus(Long userId,Long followId);

    /**
     * 访客列表
     */
    @Select(value = "SELECT u.id,u.nickname,u.sex,u.avator,u.last_login_time,u.location location,v.saw saw,v.id visitorId FROM visitor v ,`user` u WHERE v.visitor_id = #{userId} AND u.id = v.user_id AND u.deleted = 0 GROUP BY u.last_login_time")
    Page<User> seenMeList(Page<User> userPage,Long userId);

    @Select(value = "SELECT u.id,u.nickname,u.sex,u.avator,u.last_login_time,u.location location,v.saw saw,v.id visitorId FROM visitor v,`user` u WHERE user_id = #{userId} AND u.id = v.visitor_id and u.deleted = 0 GROUP BY u.last_login_time ")
    Page<User> seenOtherList(Page<User> userPage,Long userId);


    /**
     * 查询用户
     * @param userId
     * @return
     */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "profession_id", property = "professionId"),
            @Result(column = "id",property = "taohuaNum",
                one = @One(select = "com.example.speeddating.mapper.TaohuaDetailMapper.selectTaohuaSum")),
            @Result(column = "id",property = "fansNum",one = @One(select = "com.example.speeddating.mapper.FollowMapper.fansCount")),
            @Result(column = "profession_id", property = "career",
                    one = @One(select = "com.example.speeddating.mapper.ProfessionMapper.getProfessionNameById")),
            @Result(column = "id",property = "photoList",many = @Many(select = "com.example.speeddating.mapper.PhotoMapper.getPhotoByUserId"))
    })
    @Select("SELECT * FROM user WHERE id = #{id} and deleted = 0")
    public User selectByUserId(Long userId);


    /**
     * 分页用户
     * @param page
     * @return
     */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "profession_id", property = "professionId"),
            @Result(column = "id",property = "taohuaNum",
                    one = @One(select = "com.example.speeddating.mapper.TaohuaDetailMapper.selectTaohuaSum")),
            @Result(column = "id",property = "fansNum",one = @One(select = "com.example.speeddating.mapper.FollowMapper.fansCount")),
            @Result(column = "profession_id", property = "career",
                    one = @One(select = "com.example.speeddating.mapper.ProfessionMapper.getProfessionNameById")),
            @Result(column = "id",property = "photoList",many = @Many(select = "com.example.speeddating.mapper.PhotoMapper.getPhotoByUserId"))
    })
    @Select("SELECT * FROM user where deleted = 0")
    public Page<User> selectPageUser(Page<User> page);
    /**
     * 查询用户
     * @param userId
     * @return
     */
    @Select("SELECT * FROM user WHERE id = #{id} and deleted = 0")
    public User selectUser(Long userId);

    /**
     * 查询用户名
     * @param userId
     * @return
     */
    @Select(value = "SELECT nickname FROM user WHERE id = #{userId} and deleted = 0")
    public String selectUserNickname(Long userId);

    /**
     * 查询头像
     * @param userId
     * @return
     */
    @Select(value = "SELECT avator FROM user WHERE id = #{userId} and deleted = 0")
    public String selectUserAvator(Long userId);

    /**
     * 查询头像组
     * @param articleId
     * @return
     */
    @Select(value = "SELECT avator FROM `slike` l ,`user` u WHERE article_id = #{articleId} AND u.id = l.user_id AND l.deleted = 0 LIMIT 5")
    public List<String> selectUserAvatorList(Long articleId);

    @Select("SELECT * FROM user WHERE openid = #{userOpenid}")
    User selectByUserOpenid(String userOpenid);

    @Select("SELECT * FROM user WHERE email = #{email} AND password = #{password}")
    User loginByEmail(String email, String password);
}
